package com.visec.hdy.sync.ftp.code;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.visec.hdy.sync.ftp.config.SyncFileMsg;

public class FtpFileManager {

	private Map<SyncFileMsg, String> oldDataMsg;
	private Map<SyncFileMsg, String> newDataMsg;
	

	public FtpFileManager() {
		this.oldDataMsg = new HashMap<SyncFileMsg, String>();
		this.newDataMsg = new HashMap<SyncFileMsg, String>();
	}

	public synchronized boolean isLater(SyncFileMsg ftpFileMsg) {
		this.newDataMsg.put(ftpFileMsg, ftpFileMsg.getFtpFileModel().getModifyTime());
		Set<SyncFileMsg> keySet = this.oldDataMsg.keySet();
		for (SyncFileMsg key : keySet) {
			if (key.compareTo(ftpFileMsg) == 1) {
				if (key.getFtpFileModel().getFileSize() != ftpFileMsg.getFtpFileModel().getFileSize()) {
					this.oldDataMsg.remove(key);
					return false;
				}  else {
					this.oldDataMsg.remove(key);
					return true;
				}
//				else if (this.oldDataMsg.get(key).equals(ftpFileMsg.getFtpFileModel().getModifyTime())) {
//					this.oldDataMsg.remove(key);
//					return true;
//				}
			}
		}
		return false;
	}

	public synchronized void updateTime(SyncFileMsg ftpFileMsg) {
		Set<SyncFileMsg> keySet = this.newDataMsg.keySet();
		for (SyncFileMsg key : keySet) {
			if (key.compareTo(ftpFileMsg) == 1) {
				//key.getFtpFileModel().setFileSize(ftpFileMsg.getFtpFileModel().getFileSize());
				this.newDataMsg.put(key, ftpFileMsg.getFtpFileModel().getModifyTime());
				break;
			}
		}
	}

	public synchronized Set<SyncFileMsg> residueKey() {
		return this.oldDataMsg.keySet();
	}


	public synchronized void reset() {
//		System.out.println("old data:"+oldDataMsg.size());
//		System.out.println("new data:"+newDataMsg.size());
		this.oldDataMsg.clear();
		this.oldDataMsg.putAll(newDataMsg);
		this.newDataMsg.clear();
		
	}

}
